package com.yuzhenxin.coursesystem.controller.student;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.framework.common.pojo.R;
import com.yuzhenxin.coursesystem.config.EndPoints;
import com.yuzhenxin.coursesystem.controller.admin.vo.UserQueryReqVO;
import com.yuzhenxin.coursesystem.controller.student.vo.StudentCreateReqVO;
import com.yuzhenxin.coursesystem.entity.StudentDO;
import com.yuzhenxin.coursesystem.listener.StudentListener;
import com.yuzhenxin.coursesystem.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Tag(name = "学生管理")
@RestController
public class StudentController {

    @Resource
    private StudentService studentService;

    @Operation(summary = "根据id查询学生信息")
    @GetMapping(value = EndPoints.STUDENT_GETBYID)
    public R<StudentDO> getById(@RequestParam("id") Long id) {
        StudentDO studentEntity = studentService.getById(id);
        return R.success(studentEntity);
    }

    @Operation(summary = "分页条件查询学生信息")
    @PostMapping(value = EndPoints.STUDENT_SELECTPAGE, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<IPage<StudentDO>> selectPage(@Valid @RequestBody UserQueryReqVO reqDto) {
        IPage<StudentDO> studentIPage = studentService.selectPage(reqDto);
        return R.success(studentIPage);
    }

    @Operation(summary = "学生信息统计")
    @GetMapping(EndPoints.STUDENT_STATISTIC)
    public R<Map<String, String>> getStatistic() {
        Map<String, String> statistic = studentService.getStatistic();
        return R.success(statistic);
    }

    @Operation(summary = "导出数据")
    @GetMapping(EndPoints.STUDENT_EXPORTDATA)
    public void exportData(HttpServletResponse response) {
        studentService.exportData(response);
    }

    @Operation(summary = "添加学生信息")
    @PostMapping(value = EndPoints.STUDENT_INSERT, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> insert(@Valid @RequestBody StudentCreateReqVO reqVO) {
        studentService.insert(StudentDO.builder()
                .number(reqVO.getNumber())
                .password(reqVO.getPassword())
                .name(reqVO.getName())
                .sex(reqVO.getSex())
                .age(reqVO.getAge())
                .department(reqVO.getDepartment())
                .build());
        return R.success();
    }

    @Operation(summary = "添加批量学生")
    @PostMapping(value = EndPoints.STUDENT_IMPORTDATA)
    public R<?> importData(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), StudentDO.class, new StudentListener(studentService)).sheet().doRead();
        return R.success();
    }

    @Operation(summary = "修改学生信息")
    @PostMapping(value = EndPoints.STUDENT_UPDATEBYID, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> updateById(@Valid @RequestBody StudentDO studentEntity) {
        studentService.updateById(studentEntity);
        return R.success();
    }

    @Operation(summary = "删除多个学生信息")
    @DeleteMapping(value = EndPoints.STUDENT_REMOVEBYIDS, consumes = MediaType.APPLICATION_JSON_VALUE)
    public R<?> removeByIds(@Valid @RequestBody List<Long> idList) {
        studentService.removeByIds(idList);
        return R.success();
    }
}
